package com.bit.inmap;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bit.inmap.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;


public class TimeTableActivity extends Activity implements android.view.View.OnClickListener{
	private MyProgressDialog progressDialog;
	List<String> list_subject = new ArrayList<String>();
	String[] array_week = new String[] { "Time\nTable", "월요일", "화요일", "수요일",
			"목요일", "금요일" };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_time_table);
		progressDialog = MyProgressDialog.show(this,"","",true,true,null);
			
//		((Button) this.findViewById(R.id.back_btn)).setOnClickListener(this);
		String url = "http://192.168.0.202/timetable.html";				
		DownThread thread = new DownThread (url, mResHandler);	
		thread.start();		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.time_table, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		this.finish();
		
	}

	public class DownThread extends Thread {
		String mAddr;
		ResponseHandler<String> ResHandler;
		
		DownThread(String addr, ResponseHandler<String> resHandler) { 
			mAddr = addr;
			ResHandler = resHandler;
		}
		
		public void run() {
			HttpGet get = new HttpGet(mAddr);
			DefaultHttpClient client = new DefaultHttpClient();
			
			try {
				client.execute(get, mResHandler);
			} catch (Exception e) {;}
		}
	}
	
	ResponseHandler<String> mResHandler = new ResponseHandler<String>() {
		@Override
		public String handleResponse(HttpResponse response) {
			StringBuilder html = new StringBuilder();
			//Log.d("testjson", "handleResponse");
			try {
				BufferedReader br = new BufferedReader
						(new InputStreamReader(response.getEntity().getContent()));
								
				for (;;) {
					String line = br.readLine();
					if (line==null) break;
					html.append(line+'\n');
				}
				br.close();
				
				Log.d("testjson", html.toString());
				Message message = mAfterDown.obtainMessage();
				message.obj = html.toString();
				mAfterDown.sendMessage(message);
			} catch (Exception e) {;}
			return html.toString();
		}
	};	
	
	Handler mAfterDown = new Handler() {
		public void handleMessage(Message msg) {
			
			String json = (String)msg.obj;
			
			try {
				JSONArray ja = new JSONArray(json);
				
				for (int i=0 ; i<ja.length() ; i++) {
					JSONObject jo = ja.getJSONObject(i);
					
					String title = jo.getString("value");
					list_subject.add(title);
				}	
				noticeList();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}		
	};	

	private void noticeList() {
		List<String> list_week = new ArrayList<String>(Arrays.asList(array_week));
		
		GridView grid_week = (GridView) findViewById(R.id.grid_week);
		grid_week.setAdapter(new ArrayAdapter<String>(this, R.layout.grid_item_week,list_week));
		GridView grid_subject = (GridView) findViewById(R.id.grid_subject);
	
		grid_subject.setAdapter(new ArrayAdapter<String>(this, R.layout.grid_item_subject, list_subject));

		grid_subject.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

				String str = (String) parent.getAdapter().getItem(position);
					
				if(!(position % 6 == 0 || str.length()<=1)){					
					Intent intent = new Intent(TimeTableActivity.this, CurriculumActivity.class);
					intent.putExtra("msg", str );
					startActivity(intent);
				}
			}
		});
		
		if (progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}
}
